﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QL_DTTN.DTO.DTO;
using System.Linq;
using QL_DTTN.TTKT.BUS;
namespace QL_DTTN.TTKT
{
    public partial class FrmQuanLyLichThi : DevExpress.XtraEditors.XtraForm
    {
        public FrmQuanLyLichThi()
        {
            InitializeComponent();
        }
        private void FrmQuanLyLichThi_Load(object sender, EventArgs e)
        {
            cbxKyThiT_Load();
            cbxTinh_Load();
            cbxHeDaoTao_Load();
            cbxKyThi_Load();
            load_control();
            cbxTinh_SelectedIndexChanged(sender, e);
            cbxKyThi_SelectedIndexChanged(sender, e);
            cbxKyThiT_SelectedIndexChanged(sender, e);
        }
        private void cbxTinh_Load()
        {
            DataTable dt = Tinh_Bus.dataTable();
            DataRow d = dt.NewRow();
            d["maTinh"] = 0;
            d["tenTinh"] = "Tất cả";
            dt.Rows.Add(d);
            cbxTinh.DataSource = dt;
            cbxTinh.DisplayMember = "tenTinh";
            cbxTinh.ValueMember = "maTinh";
            cbxTinh.SelectedIndex = cbxTinh.Items.Count - 1;
        }
        private void cbxTinh_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (cbxTinh.SelectedIndex != -1 && cbxTinh.SelectedValue.ToString() != "System.Data.DataRowView")
                {
                    DataTable dt = Truong_Bus.dataTable(Convert.ToInt32(cbxTinh.SelectedValue));
                    DataRow d = dt.NewRow();
                    d["maTruong"] = 0;
                    d["tenTruong"] = "Tất cả";
                    dt.Rows.Add(d);
                    cbxTruong.DataSource = dt;
                    cbxTruong.DisplayMember = "tenTruong";
                    cbxTruong.ValueMember = "maTruong";
                    cbxTruong.SelectedIndex = cbxTruong.Items.Count - 1;
                }
            }
            catch
            {

            }
        }
        private void load_control()
        {
            iluTinh.DataSource = Tinh_Bus.dataTable();
            iluTinh.ValueMember = "maTinh";
            iluTinh.DisplayMember = "tenTinh";
            iluDiemThi.DataSource = DiemThi_Bus.dataTable();
            iluDiemThi.ValueMember = "maDiemThi";
            iluDiemThi.DisplayMember = "tenDiemThi";
        }
        private void cbxHeDaoTao_Load()
        {
            DataTable dt = HeDaoTao_Bus.dataTable();
            DataRow d = dt.NewRow();
            d["maHeDaoTao"] = 0;
            d["tenHeDaoTao"] = "Tất cả";
            dt.Rows.Add(d);
            cbxHeDaoTao.DataSource = dt;
            cbxHeDaoTao.DisplayMember = "tenHeDaoTao";
            cbxHeDaoTao.ValueMember = "maHeDaoTao";
            cbxHeDaoTao.SelectedIndex = cbxHeDaoTao.Items.Count - 1;
        }
        private void cbxKyThi_Load()
        {
            DataTable dt = KyThi_Bus.dataTable();
            cbxKyThi.DataSource = dt;
            cbxKyThi.ValueMember = "maKyThi";
            cbxKyThi.DisplayMember = "tenKyThi";
        }
        private void cbxKyThiT_Load()
        {
            DataTable dt = KyThi_Bus.dataTable();
            cbxKyThiT.DataSource = dt;
            cbxKyThiT.ValueMember = "maKyThi";
            cbxKyThiT.DisplayMember = "tenKyThi";
        }
        private void cbxKyThiT_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                long maKyThi = Convert.ToInt64(cbxKyThiT.SelectedValue);
                long slPhong = PhongThi_Bus.TongSo();
                long slThiSinh = ThiSinh_Bus.TongSo(maKyThi);
                lblTongPhongThi.Text = slPhong == -1 ? "Lỗi" : slPhong.ToString();
                lblTongThiSinh.Text = slThiSinh == -1 ? "Lỗi" : slThiSinh.ToString();
                long so = slThiSinh / slPhong;
                txtSoLuong.Text = (so + 5).ToString();
                DataTable dt = PhongThi_Bus.DataSoLuong(maKyThi);
                gctSoLuong.DataSource = dt;
            }
            catch
            {
            }

        }
        private void btnTao_Click(object sender, EventArgs e)
        {
            long maKyThi = Convert.ToInt64(cbxKyThiT.SelectedValue);
            int soLuong = Convert.ToInt16(txtSoLuong.Text.Trim());
            List<MonThi> lstMonThi = MonThi_Bus.lstMonThi(maKyThi);
            DataTable dtTinh = Tinh_Bus.dataTable();
            List<LichThi> lstLichThi = new List<LichThi>();
            while (lstMonThi.Count > 0)
            {
                List<MonThi> lstMT = MonThi_Bus.lstMonThi(lstMonThi[0]);
                foreach (var item in lstMT)
                {
                    var mn = lstMonThi.Where(m => m.MaMonThi == item.MaMonThi);
                    if (mn.Count() > 0)
                    {
                        lstMonThi.Remove(mn.First());
                    }
                }
                if (dtTinh != null)
                {
                    foreach (DataRow tinh in dtTinh.Rows)
                    {
                        short maTinh = Convert.ToInt16(tinh["maTinh"]);
                        DataTable phong = PhongThi_Bus.DataMaPhongThi(maTinh);
                        int sluong = soLuong;
                        #region kiemTra
                        int soPhong = phong.Rows.Count;
                        int soPhong2 = 0;
                        foreach (var item in lstMT)
                        {
                            int sl = ThiSinh_Bus.SoThiSinh(maKyThi, item.MaMonThi, maTinh);
                            soPhong2 += sl % sluong > 0 ? sl / sluong : sl + 1 / sluong;
                        }
                        if (soPhong2 > soPhong)
                        {
                            while (soPhong2 > soPhong)
                            {
                                soPhong2 = 0;
                                sluong += 2;
                                foreach (var item in lstMT)
                                {
                                    int sl = ThiSinh_Bus.SoThiSinh(maKyThi, item.MaMonThi, maTinh);
                                    soPhong2 += sl % sluong > 0 ? sl / sluong : sl + 1 / sluong;
                                }
                            }
                            XtraMessageBox.Show(tinh["tenTinh"].ToString() + " thiếu: " + (soPhong2 - soPhong) + " phòng!\n Hệ thống xử lý tăng số sinh viên mõi phòng thi của tỉnh thành: " + sluong + " thí sinh/phòng!");
                        }
                        #endregion
                        int pindex = 0;
                        foreach (var item in lstMT)
                        {
                            DataTable thisinh = ThiSinh_Bus.DataSoBaoDanh(maKyThi, item.MaMonThi, maTinh);
                            int sl2 = 0;
                            for (int i = 0; i < thisinh.Rows.Count; i++)
                            {
                                short maPhongThi = Convert.ToInt16(phong.Rows[pindex]["MaPhongThi"]);
                                sl2 = sl2 == 0 ? LichThi_Bus.SoThiSinh(maPhongThi, item.MaMonThi) : sl2;
                                while (sl2 >= sluong)
                                {
                                    pindex++;
                                    maPhongThi = Convert.ToInt16(phong.Rows[pindex]["MaPhongThi"]);
                                    sl2 = LichThi_Bus.SoThiSinh(maPhongThi, item.MaMonThi);
                                }
                                string soBaoDanh = thisinh.Rows[i]["SoBaoDanh"].ToString();
                                LichThi lichThi = new LichThi();
                                lichThi.MaMonThi = item.MaMonThi;
                                lichThi.SoBaoDanh = soBaoDanh;
                                lichThi.MaPhongThi = maPhongThi;
                                if (!LichThi_Bus.TonTai(lichThi))
                                {
                                    lstLichThi.Add(lichThi);
                                    sl2++;
                                }

                            }
                        }
                    }
                }
            }
            if (LichThi_Bus.Them(lstLichThi))
            {
                XtraMessageBox.Show("Thành công");
            }
            else
            {
                XtraMessageBox.Show("Thất bại");
            }

        }
        private void cbxKyThi_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (cbxKyThi.SelectedIndex != -1 && cbxKyThi.SelectedValue.ToString() != "System.Data.DataRowView")
                {
                    DataTable dt = MonThi_Bus.dataTable(Convert.ToInt32(cbxKyThi.SelectedValue));
                    cbxMonThi.DataSource = dt;
                    cbxMonThi.DisplayMember = "TenMon";
                    cbxMonThi.ValueMember = "MaMonThi";
                }
            }
            catch
            {

            }
        }
        private void btnTim_Click(object sender, EventArgs e)
        {
            DataTable dt = LichThi_Bus.getData(txtThongTin.Text.Trim().ToString(), Convert.ToInt16(cbxTinh.SelectedValue), cbxTruong.SelectedValue.ToString(), Convert.ToInt16(cbxHeDaoTao.SelectedValue), Convert.ToInt64(cbxKyThi.SelectedValue), Convert.ToInt64(cbxMonThi.SelectedValue));
            gctLichThi.DataSource = dt.DefaultView;
        }

    }
}